package com.ecoolex.ying.space.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.google.common.collect.Lists;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.modularize.api.service.region.Region;
import com.ecoolex.framework.modularize.api.service.region.RegionService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.user.system.entity.UserRoot;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.UserIdentityAuthentication;
import com.ecoolex.ying.space.entity.UserIdentityAuthentication.Column;
import com.ecoolex.ying.space.entity.UserInfoExt;
import com.ecoolex.ying.space.pojo.AddUserIdentityAuthenticationDTO;
import com.ecoolex.ying.space.ws.UserIdentityAuthenticationService;
import com.ecoolex.ying.space.ws.UserInfoExtService;

@GraphQL2Module(type = Type.Admin, description = "用户认证管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = UserIdentityAuthenticationGraphql.AUTH, name = "用户认证管理")
public class UserIdentityAuthenticationGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-USERIDENTITYAUTHENTICATION";
	
	@Reference
	private UserIdentityAuthenticationService userIdentityAuthenticationService;
	
	@Autowired
	private ConsumerMemberManager consumerMemberManager;
	
	@Reference
	private UserInfoExtService userInfoExtService;
	
	@Autowired
	private RegionService regionService;
	
	@TypeField(type = UserRoot.class,field = "userAuthenticationInfo",description = "用户认证信息")
	public UserIdentityAuthentication userAuthenticationInfo(@Source UserRoot source) {
		return userIdentityAuthenticationService.getByUserId(source.getUserId());
	}
	
	@TypeField(type = UserRoot.class,field = "userInfoExt",description = "用户扩展信息")
	public UserInfoExt userInfoExt(@Source UserRoot source) {
		return userInfoExtService.getByUserId(source.getUserId());
	}
	
	@TypeField(type = UserInfoExt.class,field = "region",description = "地区")
	public Region userRegion(@Source UserInfoExt source) {
		return regionService.get(source.getRegionId());
	}
	
	@TypeField(type = UserIdentityAuthentication.class,field = "userInfoExt",description = "用户扩展信息")
	public UserInfoExt userInfoExt(@Source UserIdentityAuthentication source) {
		return userInfoExtService.getByUserId(source.getUserId());
	}
	
	@TypeField(type = UserIdentityAuthentication.class,field = "user",description = "用户")
	public ConsumerMember getCommentUser(@Source UserIdentityAuthentication source) {
		return consumerMemberManager.getByUserId(source.getUserId());
	}
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "userIdentityAuthentication", description = "用户身份认证详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "用户身份认证详情")
	public UserIdentityAuthentication userIdentityAuthentication(@NonNull Integer userId,AdminMemberAuthority user) {
		user.assertLogin();
		
		return userIdentityAuthenticationService.getByUserId(userId);
	}
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "userIdentityAuthenticationList", description = "用户身份认证列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "用户身份认证列表")
	public Pagination<UserIdentityAuthentication> userIdentityAuthenticationList(Integer userIdentityAuthenticationDomainType,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertLogin();
		
		return userIdentityAuthenticationService.query(userIdentityAuthenticationDomainType, sorts, page);
	}
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addUserIdentityAuthentication", description = "添加用户身份认证")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加用户身份认证")
	public UserIdentityAuthentication add(@NonNull AddUserIdentityAuthenticationDTO add,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		ConsumerMember member = consumerMemberManager.getByUserId(add.getUserId());
		Assert.isTrue(Check.notNull(member), ResultCode.VERIFY.build(1), "用户不存在");
		return userIdentityAuthenticationService.add(add, user.userName());
	}
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editUserIdentityAuthentication", description = "编辑用户身份认证")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑用户身份认证")
	public UserIdentityAuthentication edit(@NonNull AddUserIdentityAuthenticationDTO add,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		UserIdentityAuthentication old = userIdentityAuthenticationService.getByUserId(add.getUserId());
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "用户认证信息不存在");
		
		BeanUtil.copy(add,
			old,
			Lists.newArrayList(Column.deleted, Column.createUser, Column.createTime).stream().map(
				Column::getJavaProperty).toArray(String[]::new));
		return userIdentityAuthenticationService.edit(old, user.userName());
	}
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "delUserIdentityAuthentication", description = "删除用户身份认证")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除用户身份认证")
	public boolean del(@NonNull Integer userId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		UserIdentityAuthentication old = userIdentityAuthenticationService.getByUserId(userId);
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "用户认证信息不存在");
		userIdentityAuthenticationService.delete(old, user.userName());
		return true;
	}
	
}
